package edu.whut.mall.admin.controller;

import edu.whut.mall.admin.domain.ProductCategoryParams;
import edu.whut.mall.admin.service.IProductCategoryService;
import edu.whut.mall.admin.vo.ProductCategoryVo;
import edu.whut.mall.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @program: d9lab-mall
 * @description: 商品分类控制器
 * @author: Wayne
 * @create: 2020-05-05 16:15
 **/
@Slf4j
@Api(value="商品分类控制器",tags = {"商品分类控制器"})
@RestController
@RequiredArgsConstructor
@RequestMapping("/productCategory")
public class RestProductCategoryController {

    private final IProductCategoryService productCategoryService;

    @ApiOperation("添加商品分类")
    @PostMapping(value="/addOneProductCategory")
    public CommonResult addOneProductCategory(@Valid @RequestBody ProductCategoryParams productCategoryParams){
        int result = productCategoryService.insertOneProductCategory(productCategoryParams);
        if (result >= 0) {
            return CommonResult.success("添加商品分类成功");
        }
        return CommonResult.failed("商品分类添加失败");
    }

    @ApiOperation("更新商品分类")
    @PostMapping(value="/updateOneProductCategory")
    public CommonResult updateOneProductCategory(@Valid @RequestBody ProductCategoryParams productCategoryParams){
        int result = productCategoryService.updateOneProductCategory(productCategoryParams);
        if (result >= 0) {
            return CommonResult.success("更新商品分类成功");
        }
        return CommonResult.failed("商品分类更新失败");
    }

    @ApiOperation("获取商品分类")
    @GetMapping(value="/listProductCategoryVos")
    public CommonResult listProductCategoryVos(){

        List<ProductCategoryVo> productCategoryVoList = productCategoryService.listProductCategoryVos();
        if (productCategoryVoList != null) {
            return CommonResult.success(productCategoryVoList,"获取商品分类成功");
        }
        return CommonResult.failed("获取商品分类失败");
    }
}
